import re

from beyond.Reaper import Reaper

r = Reaper()


def get_fxname(tr, i):
    return re.sub(' \\(.*\\)', '', re.sub('VSTi: ', 'VST:', r.RPR_TrackFX_GetFXName(tr, i, '', 128)[3]))


def get_param_value(param_ids, tr, i, param_name):
    return r.RPR_TrackFX_GetParamNormalized(tr, i, param_ids[get_fxname(tr, i)][param_name])


def set_param_value(param_ids, tr, i, param_name, value):
    r.RPR_TrackFX_SetParamNormalized(tr, i, param_ids[get_fxname(tr, i)][param_name], value)


def set_track_params(tr, params_str):
    for i, fx in enumerate(params_str.split(';')):
        for id, param in enumerate(fx.split(',')[1:]):
            r.RPR_TrackFX_SetParamNormalized(tr, i, id, float(param))


def get_track_params(tr):
    lst = []
    n_fx = r.RPR_TrackFX_GetCount(tr)
    for i in range(n_fx):
        id, param_name, params = 0, '?', [get_fxname(tr, i).replace(' ', '_')]
        while r.RPR_TrackFX_GetParamName(tr, i, id, '', 64)[4]:
            params.append(str(r.RPR_TrackFX_GetParamNormalized(tr, i, id)))
            id += 1
            print(r.RPR_TrackFX_GetParamName(tr, i, id, '', 64))
        lst.append(','.join(params))
    return ';'.join(lst)


Reaper.OnConnectExecute = "from reaper_python import *"

tr1 = r.RPR_GetLastTouchedTrack()


print(tr1)
print(get_track_params(tr1))

'''

    param_ids = {}

    def load_instruments(param_ids, fxname, n):
        tr = RPR_GetLastTouchedTrack()
        for i in range(n):
            RPR_TrackFX_AddByName(tr, fxname, False, i)
        id = 0
        param_name = '?'
        ids = {}
        while param_name:
            param_name = RPR_TrackFX_GetParamName(tr, 0, id, '', 64)[4]
            ids[param_name] = id
            id += 1
        param_ids[fxname] = ids

    def get_fxname(tr, i):
        return re.sub(' \\(.*\\)', '', re.sub('VSTi: ', 'VST:', RPR_TrackFX_GetFXName(tr, i, '', 128)[3]))

    def get_param_value(param_ids, tr, i, param_name):
        return RPR_TrackFX_GetParamNormalized(tr, i, param_ids[get_fxname(tr, i)][param_name])

    def set_param_value(param_ids, tr, i, param_name, value):
        RPR_TrackFX_SetParamNormalized(tr, i, param_ids[get_fxname(tr, i)][param_name], value)

    def set_track_params(tr, params_str):
        for i, fx in enumerate(params_str.split(';')):
            for id, param in enumerate(fx.split(',')[1:]):
                RPR_TrackFX_SetParamNormalized(tr, i, id, float(param))

    def get_track_params(tr):
        lst = []
        n_fx = RPR_TrackFX_GetCount(tr)
        for i in range(n_fx):
            id, param_name, params = 0, '?', [get_fxname(tr, i).replace(' ', '_')]
            while RPR_TrackFX_GetParamName(tr, i, id, '', 64)[4]:
                params.append(str(RPR_TrackFX_GetParamNormalized(tr, i, id)))
                id += 1
            lst.append(','.join(params))
        return ';'.join(lst)

    def set_track_presets(tr, presets):
        for i, preset in enumerate(presets.split(';')):
            RPR_TrackFX_SetPreset(tr, i, preset)'''